package com.uam.utils;

import com.uam.common.exception.CustomException;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author uam
 * @create 2023/11/30 13:31
 * @email uam@uam.com
 */
public class TimeUtils {
    public static String getDateTime(LocalDateTime time) {
        return time.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    }

    public static LocalDateTime getLocalDateTime(String str) {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime parse = null;
        try {
            parse = LocalDateTime.parse(str.trim(), format);
        }catch (Exception e){
            throw new CustomException("时间错误："+str);
        }

        return parse;
    }

    public static Date getDate(LocalDateTime time) {
        return Date.from(time.toInstant(ZoneOffset.of("+8")));
    }

}
